﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 租车
{
    public partial class Form1 : Form
    {
       public  Dictionary<string, Vehicle> avi;
       public  Dictionary<string, Vehicle> NOavi ;
      public void Bind(Dictionary<string,Vehicle> cd,ListView l)
      {
          ListViewItem item = null;
           l.Items.Clear();
            foreach (Vehicle c  in cd.Values)
            {
                item = new ListViewItem();
                item.Text = c.LicenseNo;
                item.SubItems.Add(c.SName);
                item.SubItems.Add(c.Color);
                item.SubItems.Add(c.YearsofService.ToString());
                item.SubItems.Add(c.DailyRent.ToString());
                if (c.Type == 1)
                    item.SubItems.Add("---");
                else
                {
                    item.SubItems.Add(c.getload().ToString());
                }
                l.Items.Add(item);
               
            }

        }
        public Form1()
        {
            InitializeComponent();
            txtLoad.Enabled = false;
            avi=new Dictionary<string,Vehicle>();
            NOavi = new Dictionary<string, Vehicle>();
          }


        private void rdTruck_CheckedChanged(object sender, EventArgs e)
        {
            txtLoad.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (rdTruck.Checked == true)
            {
                Truck t = new Truck(CmbColor.Text, Convert.ToDouble(txtDailyRent.Text), txtNO.Text, txtName.Text, Convert.ToInt32(txtYears.Text),Convert.ToInt32(txtLoad.Text));
                avi.Add(txtNO.Text,t);
                
                
            }
            else if (rdCar.Checked == true)
            {
                Car t = new Car(CmbColor.Text, Convert.ToDouble(txtDailyRent.Text), txtNO.Text, txtName.Text, Convert.ToInt32(txtYears.Text));
                avi.Add(txtNO.Text, t);
            }
            MessageBox.Show("入库成功");
        }

        private void button2_Click(object sender, EventArgs e)
        {
              string s="";
          foreach(KeyValuePair<string, Vehicle>d in NOavi)
            {
                if (d.Key == lvBack.SelectedItems[0].Text)
                {
                    int dd =Convert.ToInt32( txtRentDate.Text);
                    if(d.Value.Type==1)
                    s = d.Value.cal(dd,0).ToString();
                    else
                        s = d.Value.cal(dd, d.Value.getload()).ToString();
                  
                    avi.Add(d.Key,d.Value);
                  
                 
               

                }

            }
          NOavi.Remove(lvBack.SelectedItems[0].Text);
          Bind(NOavi, lvBack);
          MessageBox.Show("还车成功，共花费" + s);
        }

        private void lvBack_SelectedIndexChanged(object sender, EventArgs e)
        {
           // Bind(NOavi, lvBack);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            foreach (Vehicle d in avi.Values)
            {
                if (d.LicenseNo == lvin.SelectedItems[0].Text)
                {
                    d.RentUser = RentUser.Text;
                    NOavi.Add(d.LicenseNo, d);
                 
                 

                }

            }
            avi.Remove(lvin.SelectedItems[0].Text);
            Bind(avi, lvin);
            MessageBox.Show("租车成功");
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
           // Bind(avi, lvin);
        }

        private void rdCar_CheckedChanged(object sender, EventArgs e)
        {
            txtLoad.Enabled = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Bind(avi, lvin);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Bind(NOavi, lvBack);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
}
    
}
